
package com.training2.bookstore.test;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPubSub;
public class JedisSubscribeMessage extends JedisPubSub{
    public JedisSubscribeMessage() {
    	
    }
    /**
     *  接收到消息时会调用
     */
    @Override
    public void onMessage(String channel, String message) {
        System.out.println(String.format("Success receive redis published message, channel %s, message %s", channel, message));
    }
    /**
     *  订阅了频道会调用
     */
    @Override
    public void onSubscribe(String channel, int subscribedChannels) {
        System.out.println(String.format("Success subscribe redis channel success, channel %s, subscribedChannels %d",
                channel, subscribedChannels));
    }
    public static void main(String[] args) {
    	Jedis jedis = new Jedis();//如果使用空参构造，默认值 "localhost",6379端口
        String channel = "testPublish";

                jedis.subscribe(new JedisSubscribeMessage(), channel);

        
        while(true) {
        	try {
				Thread.sleep(1000000000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }
 
    }
}